package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.PriceAndNumber;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //先判断该用户是否存在相同菜品或者套餐，存在则数量加一，金额*2，不存在则开一条新的数据
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setCreateTime(LocalDateTime.now());
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //返回菜品或者套餐的数量，数量为0则该用户没有点过，数量设置为1，添加进数据库
        PriceAndNumber priceAndNumber = shoppingCartMapper.selectNumber(shoppingCart);
        if (priceAndNumber == null){
            //该之前用户没有该商品
            shoppingCart.setNumber(1);
            BigDecimal price;
            if (shoppingCart.getDishId() != null){
                //点的是菜品,查询菜品id获取金额
                Dish dish = dishMapper.selectPrice(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                price = dish.getPrice();
            }else {
                //点的是套餐，查询套餐id获取金额
                Setmeal setmeal = setmealMapper.selectPrice(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                price = setmeal.getPrice();
            }
            shoppingCart.setAmount(price);
            shoppingCartMapper.add(shoppingCart);
        }else {
            BigDecimal amount = priceAndNumber.getAmount();
            Integer number = priceAndNumber.getNumber();
            number = number+1;
            //amount = amount.add(amount);
            shoppingCart.setNumber(number);
            shoppingCart.setAmount(amount);
            shoppingCartMapper.update(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * */
    @Override
    public List<ShoppingCart> list() {
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.findAll(BaseContext.getCurrentId());
        return shoppingCartList;
    }

    /**
     * 删除购物车中一个商品
     * */
    @Override
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        PriceAndNumber priceAndNumber = shoppingCartMapper.selectNumber(shoppingCart);
        Integer number = priceAndNumber.getNumber();
        if (number==1){
            //直接删除
            shoppingCartMapper.deleteOne(shoppingCart);
        }else {
            //数量减一，价格减去自身
            number = number-1;
            BigDecimal amount = priceAndNumber.getAmount();
            //amount = amount.subtract(amount);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(number);
            shoppingCart.setAmount(amount);
            shoppingCartMapper.update(shoppingCart);
        }
    }

    /**
     * 清空购物车
     * */
    @Override
    public void deleteAll() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.deleteOne(shoppingCart);
    }
}
